package JDBC_Demo;

import org.junit.Test;
import pojo.Account;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class  JDBCDemo2 {
    public static final String URL = "jdbc:mysql://localhost:3306/test01?useSSL=false";
    public static final String USER = "root";
    public static final String PASSWORD = "";
    @Test
    public void selectAccount() throws Exception {
        //1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获得数据库连接
        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        //3.操作数据库，实现增删改查
        Statement stmt = conn.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT id,name,money FROM tb_account");
        List<Account> accountList=new ArrayList<>();
        //如果有数据，rs.next()返回true
        while(rs.next()){
            Account account = new Account();
            int id = rs.getInt("id");
            String name = rs.getString("name");
            BigDecimal money = rs.getBigDecimal("money");
            account.setId(id);
            account.setName(name);
            account.setMoney(money);
            accountList.add(account);
        }

        System.out.println(accountList);

        rs.close();
        stmt.close();
        conn.close();
    }
}
